-- xmonad-default-18.1-tweaking-050.hs
-- -- even tho the llm got one to compile,
----- i'm carrying on manually doing it.
import XMonad
import Data.Monoid
import System.Exit

import qualified XMonad.StackSet as W
import qualified Data.Map        as M

import XMonad.Layout
import XMonad.Layout.OneBig
import XMonad.Layout.GridVariants
import XMonad.Actions.GridSelect

--https://hackage.haskell.org/package/xmonad-contrib-0.17.0/docs/XMonad-Layout-Tabbed.html
import XMonad.Layout.Tabbed
--https://xmonad.github.io/xmonad-docs/xmonad-contrib/XMonad-Layout-SubLayouts.html
import XMonad.Layout.SubLayouts
--import XMonad.Layout.WindowNavigation
import qualified XMonad.Layout.WindowNavigation as WN -- hrmm kinda essential, it seems.
--import XMonad.Layout.BoringWindows
import XMonad.Layout.NoBorders -- prettier when fullscreen
import XMonad.Layout.MultiToggle -- allows fullscreen toggle, without Full layout
import XMonad.Actions.WindowNavigation
import XMonad.Layout.BoringWindows -- not so boring, proves handy.  make it so.

import XMonad.Util.NamedScratchpad
import XMonad.ManageHook

-- testing the old llm tabular imports again, see if that mends the not in scop for MIRROR TALL FULL etc
-- trimming duplicates
import XMonad.Layout.LayoutCombinators
import XMonad.Util.EZConfig
import XMonad.Actions.Submap

import XMonad.Actions.CycleWS

import XMonad.Layout.MultiToggle.Instances
import XMonad.Actions.DynamicWorkspaces
import XMonad.Actions.CopyWindow (copy)

-- well... importing all that remedied the out of scope for MIRROR TALL and FULL etc.   ... no idea why though...  something that wasnt in the list prior.

main = xmonad defaults

defaults = def {
      -- simple stuff
        terminal           = dTerm,
        focusFollowsMouse  = mouseFocus,
        clickJustFocuses   = clickFocus,
        borderWidth        = borderPix,
        modMask            = modKey,
        workspaces         = workSpaces,
        normalBorderColor  = borderColor,
        focusedBorderColor = focusedWinBorder,

      -- key bindings
        keys               = keyBinds,
        mouseBindings      = mouseBinds,

      -- hooks, layouts
--        layoutHook         = myLayout,
--        layoutHook         = tabulargranpalayoutHook,
        layoutHook         = djtslayoutHook,
        manageHook         = floatHooks,
        handleEventHook    = myEventHook,
        logHook            = myLogHook,
        startupHook        = myStartupHook
    }

scratchpads = [
    NS "ttyload" "xterm -e ttyload" (title =? "ttyload")
        (customFloating $ W.RationalRect (0/1) (0/1) (1/1) (1/2)),
    NS "htop" "xterm -e htop" (title =? "htop")
        (customFloating $ W.RationalRect (0/1) (0/1) (1/1) (1/2)),
    NS "alsamixer" "xterm -e alsamixer" (title =? "alsamixer")
        (customFloating $ W.RationalRect (1/100) (49/100) (98/100) (1/2)),
    NS "emacs" "emacs" (className =? "Emacs")
        (customFloating $ W.RationalRect (1/100) (49/100) (98/100) (1/2)),
--    NS "xterm" "xterm -e tmux" (title =? "tmux")
--        (customFloating $ W.RationalRect (0/1) (0/1) (1/1) (1/2)),
    NS "st" "st -e tmux" (title =? "tmux")
        (customFloating $ W.RationalRect (0/1) (0/1) (1/1) (1/2)),
    NS "firefox" "firefox" (className =? "Firefox")
        (customFloating $ W.RationalRect (0/1) (0/1) (1/1) (1/2)),
    NS "chat" "iirc" (title =? "chat")
        (customFloating $ W.RationalRect (0/1) (0/1) (1/2) (1/2))
    ] where role = stringProperty "WM_WINDOW_ROLE"

dTerm      = "st"

mouseFocus :: Bool
mouseFocus = True

clickFocus :: Bool
clickFocus = False -- false means it clicks the click the window would recieve without needing a click to focus first.  u want that.  :)  most of the time.

borderPix  = 1

modKey     = mod4Mask -- mod4mask = windows key
--
workSpaces = ["1","2","3","4","5","6","7","8","9"]

borderColor  = "#575757"
focusedWinBorder = "#fff257"

keyBinds conf@(XConfig {XMonad.modMask = modm}) = M.fromList $
      --((modm .|. shiftMask, xK_Return), spawn $ XMonad.terminal conf)    -- launch a terminal
    [ ((modm,               xK_Return), spawn $ XMonad.terminal conf)    -- launch a terminal
    , ((modm,               xK_r     ), spawn "dmenu_run")    -- launch dmenu
--    , ((modm .|. shiftMask, xK_p     ), spawn "gmrun")    -- launch gmrun
    , ((modm .|. shiftMask, xK_F4     ), kill)    -- close focused window
    , ((modm,               xK_space ), sendMessage NextLayout)     -- Rotate through the available layout algorithms
    , ((modm .|. shiftMask, xK_space ), setLayout $ XMonad.layoutHook conf)    --  Reset the layouts on the current workspace to default (idk if i want)
--    , ((modm,               xK_n     ), refresh)    -- Resize viewed windows to the correct size
    , ((modm,               xK_Tab   ), windows W.focusDown)    -- Move focus to the next window
    , ((modm .|. shiftMask, xK_Tab   ), windows W.focusUp)    -- Move focus to the next window
-- move focus
    , ((modm,               xK_j     ), windows W.focusDown)    -- Move focus to the next window
    , ((modm,               xK_d     ), windows W.focusDown)    -- Move focus to the next window
    , ((modm,               xK_k     ), windows W.focusUp)    -- Move focus to the previous window
    , ((modm,               xK_e     ), windows W.focusUp)    -- Move focus to the previous window
--    , ((modm,               xK_m     ), windows W.focusMaster  )    -- Move focus to the master window
--    , ((modm,               xK_Return), windows W.swapMaster)    -- Swap the focused window and the master window
-- move windows
    , ((modm .|. shiftMask, xK_j     ), windows W.swapDown  )    -- Swap the focused window with the next window
    , ((modm .|. shiftMask, xK_d     ), windows W.swapDown  )    -- Swap the focused window with the next window
    , ((modm .|. shiftMask, xK_k     ), windows W.swapUp)    -- Swap the focused window with the previous window
    , ((modm .|. shiftMask, xK_e     ), windows W.swapUp)    -- Swap the focused window with the previous window
-- this'll be move window left/right stuff
--    , ((modm,               xK_h     ), sendMessage Shrink)    -- Shrink the master area
--    , ((modm,               xK_s     ), sendMessage Shrink)    -- Shrink the master area
--    , ((modm,               xK_l     ), sendMessage Expand)    -- Expand the master area
--    , ((modm,               xK_f     ), sendMessage Expand)    -- Expand the master area
    , ((modm,               xK_t     ), withFocused $ windows . W.sink)    -- Push window back into tiling
    , ((modm              , xK_comma ), sendMessage (IncMasterN 1))    -- Increment the number of windows in the master area
    , ((modm              , xK_period), sendMessage (IncMasterN (-1))) -- Deincrement the number of windows in the master area
    -- , ((modm              , xK_b     ), sendMessage ToggleStruts)    -- Toggle the status bar gap
    , ((modm .|. shiftMask, xK_q     ), io exitSuccess)    -- Quit xmonad
    , ((modm              , xK_q     ), spawn "xmonad --recompile; xmonad --restart")    -- Restart xmonad
    , ((modm .|. shiftMask, xK_slash ), xmessage help)    -- Run xmessage with a summary of the default keybindings (useful for beginners)
--    , ("M-<F11>", sendMessage $ Toggle FULL)
--    , ("M-S-<F11>", sendMessage $ Toggle MIRROR)
--    , ("M-g", sendMessage $ Toggle FULL)
--    , ("M-S-g", sendMessage $ Toggle MIRROR)
-- see how much nicer that^ kind of config is...
    , ((modm, xK_F11), sendMessage $ Toggle FULL)
    , ((modm .|. shiftMask, xK_F11), sendMessage $ Toggle MIRROR)
    , ((modm, xK_g), sendMessage $ Toggle FULL)
    , ((modm .|. shiftMask, xK_g ), sendMessage $ Toggle MIRROR)
    , ((modm, xK_F4), kill)
    , ((modm, xK_F6), spawn "xkill")
--    , ((modm .|. controlMask, xK_r) spawnSelected def {gs_cellheight = 42, gs_cellwidth = 180} ["gimp","mypaint","inkscape","blender","freecad","lmms","hydrogen","audacity","gcolor2","fontforge","openshot","digitclock"]) -- broken, didnt need anyways
    , ((modm .|. controlMask, xK_s), sendMessage $ pullGroup WN.L)
    , ((modm .|. controlMask, xK_f), sendMessage $ pullGroup WN.R)
    , ((modm .|. controlMask, xK_e), sendMessage $ pullGroup WN.U)
    , ((modm .|. controlMask, xK_d), sendMessage $ pullGroup WN.D)
    , ((modm,               xK_w), windows W.swapMaster)
    , ((modm .|. controlMask, xK_w), withFocused (sendMessage . UnMerge))
    , ((modm .|. controlMask .|. shiftMask, xK_w), withFocused (sendMessage . MergeAll))
    , ((modm, xK_c), prevWS)
    , ((modm, xK_v), nextWS)
    , ((modm .|. shiftMask, xK_c), shiftToPrev)
    , ((modm .|. shiftMask, xK_v), shiftToNext)
      -- , ("S-M-c", shiftToPrev)
     -- , ("S-M-v", shiftToNext)
-- scratchpads!  yay!  :D
         -- , ("<F12>", namedScratchpadAction scratchpads "xterm") -- may repeat as mod `, and change from xterm to st.  :)
--  --template--:     , ((modm, xK_),)
    , ((modm, xK_F12), namedScratchpadAction scratchpads "st") -- win-F12 main terminal scratchpad
    , ((modm, xK_grave), namedScratchpadAction scratchpads "st") -- win-` main terminal scratchpad
     -- , ("M-<F1>", namedScratchpadAction scratchpads "emacs")
     -- , ("M-<F2>", namedScratchpadAction scratchpads "firefox") -- may change to ... librewolf?  or brave?  ... or dillo!?
     -- , ("M-<F7>", namedScratchpadAction scratchpads "ttyload")
     -- , ("M-<F8>", namedScratchpadAction scratchpads "htop") -- may change to retopper?  
     -- , ("M-<F9>", namedScratchpadAction scratchpads "alsamixer")

    ]
    ++
    -- mod-[1..9], Switch to workspace N
    -- mod-shift-[1..9], Move client to workspace N
    [((m .|. modm, k), windows $ f i)
        | (i, k) <- zip (XMonad.workspaces conf) [xK_1 .. xK_9]
        , (f, m) <- [(W.greedyView, 0), (W.shift, shiftMask)]] 
   -- ++
    -- mod-{w,e,r}, Switch to physical/Xinerama screens 1, 2, or 3
    -- mod-shift-{w,e,r}, Move client to screen 1, 2, or 3 -- ah-ha!  that's why focus up wasnt working... not on multi monitor just now 
--    [((m .|. modm, key), screenWorkspace sc >>= flip whenJust (windows . f))
--        | (key, sc) <- zip [xK_w, xK_e, xK_r] [0..]
--        , (f, m) <- [(W.view, 0), (W.shift, shiftMask)]]

mouseBinds (XConfig {XMonad.modMask = modm}) = M.fromList
    [ ((modm, button1), \w -> focus w >> mouseMoveWindow w
                                      >> windows W.shiftMaster)    -- mod-button1, Set the window to floating mode and move by dragging
    , ((modm, button2), \w -> focus w >> windows W.shiftMaster)    -- mod-button2, Raise the window to the top of the stack
    , ((modm, button3), \w -> focus w >> mouseResizeWindow w
                                      >> windows W.shiftMaster)    -- mod-button3, Set the window to floating mode and resize by dragging
    ]

-- layouthooks

tabulargranpalayoutHook =                    (Grid (55/34) ||| OneBig (5/8) (3/5) ||| OneBig (3/4) (3/4))

djtslayoutHook = smartBorders 
                 $ WN.windowNavigation 
                 $ subTabbed 
                 $ boringWindows 
                 $
                 mkToggle (MIRROR ?? NOBORDERS ?? FULL ?? EOT) (Grid (55/34) ||| OneBig (5/8) (3/5) ||| OneBig (3/4) (3/4))
                 
floatHooks = composeAll
    [ className =? "MPlayer"        --> doFloat
--    , className =? "Gimp"           --> doFloat
    , resource  =? "desktop_window" --> doIgnore
    , resource  =? "kdesktop"       --> doIgnore ]

myEventHook = mempty

myLogHook = return ()

myStartupHook = return ()

-- | Finally, a copy of the default bindings in simple textual tabular format.
help :: String
help = unlines ["The default modifier key is 'alt'. Default keybindings:",
    "",
    "-- launching and killing programs",
    "mod-Shift-Enter  Launch xterminal",
    "mod-p            Launch dmenu",
    "mod-Shift-p      Launch gmrun",
    "mod-Shift-c      Close/kill the focused window",
    "mod-Space        Rotate through the available layout algorithms",
    "mod-Shift-Space  Reset the layouts on the current workSpace to default",
    "mod-n            Resize/refresh viewed windows to the correct size",
    "mod-Shift-/      Show this help message with the default keybindings",
    "",
    "-- move focus up or down the window stack",
    "mod-Tab        Move focus to the next window",
    "mod-Shift-Tab  Move focus to the previous window",
    "mod-j          Move focus to the next window",
    "mod-k          Move focus to the previous window",
    "mod-m          Move focus to the master window",
    "",
    "-- modifying the window order",
    "mod-Return   Swap the focused window and the master window",
    "mod-Shift-j  Swap the focused window with the next window",
    "mod-Shift-k  Swap the focused window with the previous window",
    "",
    "-- resizing the master/slave ratio",
    "mod-h  Shrink the master area",
    "mod-l  Expand the master area",
    "",
    "-- floating layer support",
    "mod-t  Push window back into tiling; unfloat and re-tile it",
    "",
    "-- increase or decrease number of windows in the master area",
    "mod-comma  (mod-,)   Increment the number of windows in the master area",
    "mod-period (mod-.)   Deincrement the number of windows in the master area",
    "",
    "-- quit, or restart",
    "mod-Shift-q  Quit xmonad",
    "mod-q        Restart xmonad",
    "mod-[1..9]   Switch to workSpace N",
    "",
    "-- Workspaces & screens",
    "mod-Shift-[1..9]   Move client to workspace N",
    "mod-{w,e,r}        Switch to physical/Xinerama screens 1, 2, or 3",
    "mod-Shift-{w,e,r}  Move client to screen 1, 2, or 3",
    "",
    "-- Mouse bindings: default actions bound to mouse events",
    "mod-button1  Set the window to floating mode and move by dragging",
    "mod-button2  Raise the window to the top of the stack",
    "mod-button3  Set the window to floating mode and resize by dragging"]
